/*
 * Copyright (c) 2018-2023 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#ifndef BHISTOUT_H
#define BHISTOUT_H

#include "hash.h"
#include "subcmd.h"

#define OPT_ALL           0x1
#define OPT_DFTSTATUS     0x2
#define OPT_DONE          0x4
#define OPT_PEND          0x8
#define OPT_SUSP          0x10
#define OPT_RUN           0x20
#define OPT_QUEUE         0x40
#define OPT_USER          0x80
#define OPT_ALLUSERS      0x100
#define OPT_HOST          0x200
#define OPT_JOBID         0x800
#define OPT_COMPLETE      0x1000
#define OPT_SUBMIT        0x2000
#define OPT_DISPATCH      0x4000
#define OPT_ELOGFILE      0x8000
#define OPT_JOBNAME	  0x20000
#define OPT_NUMLOGFILE    0x40000
#define OPT_PROJ          0x80000
#define OPT_ALLPROJ       0x100000
#define OPT_CHRONICLE     0x200000
#define OPT_TIME_INTERVAL 0x400000
#define OPT_ARRAY_INFO    0x800000
#define OPT_EXIT          0x1000000

struct recordRequest {
    int options;
    uid_t user_id;
    char *user_name;
    char record_file_name[PATH_MAX];
    char queue[MAXNAMELEN];
    char node[MAXHOSTNAMELEN];
    time_t end_time[2];
    time_t submit_time[2];
    time_t start_time[2];
    time_t search_time[2];
    unsigned int num_job;
    JOBID_T *job_ids;
    char *job_name;
    char *project_name;
    unsigned int num_min_file;
    unsigned int num_max_file;
};

struct recordContent {
    struct recordContent *next;
    struct recordJob *job;
    char kind;
    int job_status;
    int reasons;
    int subreasons;
    char queue[MAXNAMELEN];
    time_t time_stamp;
    unsigned int num_execute_node;
    char **execute_nodes;
    unsigned int *num_slot;
    pid_t job_pid;
    int act_flags;
    int chkpnt_period;
    int action_status;
    int action_pid;
    unsigned int num_spec_node;
    char **spec_nodes;
    struct QuickPoolJobInfo *modify_params;
    float cpu_time;
    uid_t user_id;
    char *user_name;
    char *signal_symbol;
    uid_t execute_uid;
    char *execute_home;
    char *execute_cwd;
    char *execute_user;
    int exit_code;
    int base;
    int position;
    struct jobInfoGPU *job_GPU;
    struct jobInfoNuma *job_numa;
};

struct recordJob {
    LIST_T *forw;
    LIST_T *back;
    struct QuickPoolJobInfo *job;
    pid_t job_pid;
    pid_t job_gid;
    int current_status;
    time_t delay_time;
    char *host;
    pid_t precmd_pid;
    pid_t precmd_pgid;
    struct recordContent *record_head;
    struct recordContent *record_tail;
};

extern HASH_TABLE_T *ghashJobRecord;
extern LIST_T *glistJobRecord;
extern struct recordRequest request;

extern struct QuickPoolJobInfo *read_jobcreate(struct record *);
extern struct recordJob **read_jobmodify(struct record *, int *, struct recordRequest *);
extern struct recordJob *read_jobrun(struct record *, struct recordRequest *);
extern struct recordJob *read_jobstarted(struct record *, struct recordRequest *);
extern struct recordJob *read_jobstatus(struct record *, struct recordRequest *);
extern struct recordJob *read_jobswitch(struct record *, struct recordRequest *);
extern struct recordJob *read_jobmove(struct record *, struct recordRequest *);
extern struct recordJob *read_jobstarting(struct record *, struct recordRequest *);
extern struct recordJob *read_jobrequeue(struct record *, struct recordRequest *);
extern struct recordJob *read_jobclean(struct record *, struct recordRequest *);
extern struct recordJob *read_jobmigrate(struct record *, struct recordRequest *);
extern struct recordJob *read_jobsignal(struct record *, struct recordRequest *);
extern struct recordJob *read_jobsigact(struct record *, struct recordRequest *);
extern struct recordJob *read_jobforcerun(struct record *, struct recordRequest *);
extern struct recordJob *read_createpod(struct record *, struct recordRequest *);
extern struct recordJob *read_bindpod(struct record *, struct recordRequest *);
extern struct recordJob *read_deletepod(struct record *, struct recordRequest *);

extern void free_jobrecord(struct recordJob *);

extern int get_job_idxlist(char *, int **);
extern bool_t is_request_jobid(struct recordRequest *, JOBID_T);

extern void parse_record(struct record *, struct recordRequest *);
#endif
